import java.util.Vector;


public class IRCCommand {
	private String command;
	private String nick;
	private String hostname;
	private String user;
	private Boolean numeric;
	private int numCommand;
	private Vector<String> parameters;
	
	public IRCCommand() {
		command = "";
		nick = "";
		hostname = "";
		user = "";
		numCommand = -1;
		numeric = false;
		parameters = new Vector<String>(15);
	}

	public String getCommand(){
		if (numeric) {
			return numCmdToString();
		}
		return command;
	}
	
	public void setCommand(String command) {
		numeric = false;
		numCommand = -1;
		this.command = command;
	}
	
	public int getNumCommand() {
		return numCommand;
	}
	
	public void setNumCommand(int numcmd) {
		command = "";
		numeric = true;
		numCommand = numcmd;
	}
	
	public Boolean isNumeric() {
		return numeric;
	}
	
	public String getPrefix(){
		if (this.nick != "" && this.user != "") {
			return this.nick + "!" + this.user + "@" + this.hostname;
		}
		else {
			return this.hostname;
		}
	}
	
	public void setPrefix(String prefix){
		int i = prefix.indexOf("!");
		int j = prefix.indexOf("@");
		
		if (i != -1 && j != -1) {
			this.nick = prefix.substring(0, i);
			this.user = prefix.substring(i+1, j);
			this.hostname = prefix.substring(j+1);
		}
		else {
			this.hostname = prefix;
		}
	}
	
	public String getNick() {
		return nick;
	}
	
	public void setNick(String nick) {
		this.nick = nick;
	}
	
	public String getUser() {
		return user;
	}
	
	public void setUser(String user) {
		this.user = user;
	}
	
	public String getHost() {
		return nick;
	}
	
	public void setHost(String host) {
		this.hostname = host;
	}
	
	public void addParameter(String param) {
		parameters.add(param);
	}
	
	public String getParameter(int i) {
		if (i < parameters.size())
			return parameters.get(i);
		return null;
	}
	
	public int numberOfParameters() {
		return parameters.size();
	}
	
	private String getMiddle(){
		String middle = "";
		for (int i = 0; i < parameters.size()-1; i++) {
			middle += parameters.get(i);
			if (i < parameters.size()-2) {
				middle += " ";
			}
		}
		return middle;
	}
	
	
	public String getTrailing(){
		return parameters.lastElement();
	}
	
	public String getString(){
		String str = "";
		String prefix = getPrefix();
		if (!prefix.equals("")){
			str += ":" + prefix;
		}
		str += " " + getCommand() + " " + getMiddle();
		String trail = getTrailing();
		if (!trail.equals("")){
			str += " :" + trail;
		}
		str += "\r\n";
		return str;
	}
	
	private String numCmdToString() {
		if (!numeric){
			return "";
		}
		String zeros = "";
		if (numCommand < 100){
			zeros += "0";
			if (numCommand < 10){
				zeros += "0";
			}
		}
		return zeros + numCommand;
	}

}
